import {
  get
} from 'lodash'
export default {
  methods: {
    limitNameLength (rule, value, callback) {
      if (value.length > 85) {
        return callback(new Error(this.$t('custom.accountMapping.nameLimit')))
      }
      callback()
    },
    limitLargeNameLength (rule, value, callback) {
      if (value.length > 256) {
        return callback(new Error(this.$t('custom.accountMapping.largeNameLimit')))
      }
      callback()
    },
    async validatePass (rule, value, callback) {
      const {
        postCheckNameApi
      } = this.$interface.reports
      const accountId = get(this.form, 'id', '')
      let params = {
        name: value,
        isNew: !accountId,
        accountId
      }
      const {
        data
      } = await postCheckNameApi(params)
      if (data.code !== 0) {
        return callback(new Error(this.$t('custom.accountMapping.nameExists')))
      }
      callback()
    },
    checkCopyFrom (rule, value, callback) {
      if (!value && !!get(this.form, 'copy', false)) {
        return callback(new Error(this.$t('custom.component.gadget.select')))
      }

      callback()
    }
  }
}
